/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardHouse.cards;

import java.util.EnumSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author samuel
 */
public class Deck {
    private final int     MAX_NUMBER_OF_CARDS = 51; // Maximum number of cards in the deck
    private int           m_currentIndex;
    private Card[] m_cardContainer;
    
    /**
     *
     */
    public Deck(){
        m_currentIndex = 0;
        m_cardContainer = new Card[52];
       
        Set<Card.FaceValue> valueSet = EnumSet.allOf(Card.FaceValue.class);
        Set<Card.Suit> suitSet = EnumSet.allOf(Card.Suit.class);
        Iterator<Card.FaceValue> valueIterator = valueSet.iterator();
         
        while(valueIterator.hasNext()){
            Card.FaceValue cardValue = valueIterator.next();
            Iterator<Card.Suit> suitIterator = suitSet.iterator();
            while(suitIterator.hasNext()){
               Card.Suit suitValue = suitIterator.next();
               Card freshCard = new Card();
               freshCard.setFaceValue(cardValue);
               freshCard.setSuit(suitValue);
               m_cardContainer[m_currentIndex]=freshCard;
               m_currentIndex ++;
            }//while
        }//while
        m_currentIndex = 0;
    }
    
    // allows dealer to draw a card from the top of the deck
    public Card drawCard(){      
        m_cardContainer[m_currentIndex].setDrawn(true);
        Card card = m_cardContainer[m_currentIndex];
        if(m_currentIndex<51){
            m_currentIndex++;
        }
        return card;
    } 
    
     public Card drawCard(int index){      
        m_cardContainer[index].setDrawn(true);
        Card card = m_cardContainer[index];
        return card;
     }
     
     protected int indexOfCurrentCard(){
        return m_currentIndex;
    }
    protected int numberOfCardsRemaining(){
        int remaining_cards = 52;
        for(int index = 0; index < 52; index++){
            if(m_cardContainer[index].getisDrawn()){
                remaining_cards --;
            }
        }
        return remaining_cards;
    } 
    
    public void collectDeck(){
        for(m_currentIndex = 0; m_currentIndex<52; m_currentIndex ++){
            m_cardContainer[m_currentIndex].setDrawn(false);
        }
        m_currentIndex = 0;
    }
    
    protected boolean checkDrawn(int index){
        return m_cardContainer[index].getisDrawn();
    }
    
    /**
     *
     * @return
     */
    public Card dealRandomCard(){
        Random shuffler = new Random();
        int randomIndex = shuffler.nextInt(51);
        while(this.checkDrawn(randomIndex)){
            randomIndex = shuffler.nextInt(52);
        }
        return this.drawCard(randomIndex);
    }
    //todo: is it really removing from tempPack?
    public void shuffleDeck(){
        Deck tempPack = new Deck();
        int nextCardIndex;
        Random shuffler = new Random();
        for(int i=0; i<=MAX_NUMBER_OF_CARDS; i++){
            do{
            nextCardIndex = shuffler.nextInt(52);
            }while(tempPack.checkDrawn(nextCardIndex));
            m_cardContainer[i] = tempPack.drawCard(nextCardIndex);
            m_cardContainer[i].setDrawn(false);
        }//for
        m_currentIndex = 0;
    }

}

